<?php

namespace app\admin\controller;

use app\admin\model\Withdraw as WithdrawService;
use app\model\AgentWithdrawConfig;
use think\Request;

class Withdraw extends Controller
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @api {GET} /admin/withdraw/list  提现列表
     * @apiName withdrawList  提现列表
     * @apiDescription 提现列表
     * @apiGroup Admin2_0
     * @apiParam {Number=1,2,3,4,5,6} [status] 提现状态
     * @apiParam {Number} [page=1] 页码
     * @apiParam {Number} [list_rows=10] 分页大小
     * @apiUse PageReturn
     * @apiVersion 1.0.0
     */
    public function list()
    {
        $service = new WithdrawService();
        $data = input('post.');
        if (!empty($data['is_export'])) {
            return $service->getList($data);
        }

        return success('操作成功', $service->getList($data, 1));
    }

    /**
     * @api {GET} /admin/withdraw/accept 审核提现
     * @apiName withdrawAccept  审核提现
     * @apiDescription 审核提现
     * @apiGroup Admin2_0
     * @apiParam {Number} id
     * @apiUse PageReturn
     * @apiVersion 1.0.0
     */
    public function accept(Request $request)
    {
        $id = $request->param('id');
        if (!$id) {
            return fail('缺少必要参数');
        }

        // 业务处理
        $service = new WithdrawService();
        $res = $service->accept($id);
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }

    /**
     * @api {GET} /admin/withdraw/reject 拒绝审核提现
     * @apiName withdrawReject  拒绝审核提现
     * @apiDescription 拒绝审核提现
     * @apiGroup Admin2_0
     * @apiParam {Number} id
     * @apiParam {String} [remark] 备注
     * @apiUse PageReturn
     * @apiVersion 1.0.0
     */
    public function reject(Request $request)
    {
        // step 参数校验
        $rules = [
            'id' => 'require|number',
            'remark' => 'require',
        ];
        $messages = [
            'require' => '缺少必要参数',
            'number' => '非法参数格式',
        ];
        $param = $request->only(['id', 'remark']);
        if ($msg = dmjValidate($rules, $messages, $param)) {
            return fail($msg);
        }
        if (mb_strlen($param['remark']) > 255) {
            return fail('审核不通过原因不能超过 255 个字符');
        }

        // 业务处理
        $service = new WithdrawService();

        $res = $service->reject($param);
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }

    /**
     * @api {GET} /admin/withdraw/getConfig 提现配置获取
     * @apiDescription 提现配置获取
     * @apiName 提现配置获取
     * @apiGroup Admin2_0
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function getConfig()
    {
        $data = AgentWithdrawConfig::where('agent_id', 0)->find();
        if ($data) {
            $data = $data->toArray();
        } else {
            $data = ['id' => 0];
        }

        return success('', $data);
    }

    /**
     * @api {POST} /admin/withdraw/setConfig 配置提现
     * @apiDescription 配置提现
     * @apiName 配置提现
     * @apiParam {Number} id id，根据 getConfig获取到的id
     * @apiParam {Number} min_amount 最低提现金额 单位元
     * @apiParam {Number} day_max_freq 单日最大提现次数
     * @apiParam {Number} rate 提现手续费
     * @apiParam {Number} auto_aprove_max 自动审核金额封顶金额
     * @apiParam {String} [memo]  备注
     * @apiGroup Admin2_0
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function setConfig(Request $request)
    {
        $rules = [
            'min_amount' => 'require|float|gt:0',
            'rate' => 'require|float|egt:0',
            'auto_aprove_max' => 'require|float|gt:0',
            'day_max_freq' => 'require|number|gt:0',
        ];
        $messages = [
            'min_amount.require' => '请填写最低提现金额',
            'min_amount.float' => '最低提现金额只能是数字',
            'min_amount.gt' => '最低提现金额必须大于 0',
            'day_max_freq.require' => '请填写单日最多提现次数',
            'day_max_freq.number' => '单日最多提现次数只能是整数',
            'day_max_freq.gt' => '单日最多提现次数必须大于 0',
            'auto_aprove_max.require' => '请填写自动审核金额',
            'auto_aprove_max.float' => '自动审核金额只能是数字',
            'auto_aprove_max.gt' => '自动审核金额必须大于 0',
            'rate.require' => '请填写提现手续费',
            'rate.float' => '提现手续费只能是数字',
            'rate.egt' => '提现手续费不能小于 0',
        ];
        $config = $request->only(['min_amount', 'day_max_freq', 'rate', 'auto_aprove_max', 'memo' => '']);
        if ($msg = dmjValidate($rules, $messages, $config)) {
            return fail($msg);
        }

        $id = $request->param('id', 0);
        if (!$id) {  // 新建
            $configModel = new AgentWithdrawConfig();
        } else {
            $configModel = AgentWithdrawConfig::find($id);
            if (!$configModel) {
                return fail('数据不存在，请刷新或者联系我们');
            }
        }

        foreach ($config as $k => $v) {
            $configModel->{$k} = $v;
        }
        if (!$configModel->save()) {
            return fail('操作失败，请重试或者联系我们');
        }

        return success();
    }
}
